#!/bin/bash

# Automatically set Git config values from parent folders, until GIT ROOT is reach.
# there could be issue when there are .gitconfig in differnt folders, the result will deepend on from where checkout is triggered.

IFS=$'\n'
dir=$(pwd)
gitroot=$(git rev-parse --show-toplevel)

while [ "$dir" != "/" ]; do
    if [ -f $dir/.gitconfig ]; then
        for config in $(git config --file $dir/.gitconfig --list); do
            var=$(echo $config | cut -d = -f 1)
            value=$(echo $config | cut -d = -f 2-)
            actual=$(git config $var)
            if [ "$actual" != "$value" ]; then
                git config $var $value
            fi
        done
        break
    fi

    if [ "$dir" == "$gitroot" ]; then
        break
    fi

    # next parent folder
    dir=$(dirname $dir)
done

#
# to check the repo specific setting.
# if the repo script return none 0, the operation may fail.
scriptdir=`dirname "$0"`
$scriptdir/common-op `basename "$0"` $@
